//
//  MyAssetViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/24.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "MyAssetViewController.h"
#import "MyAssetHeaderView.h"
#import "MyAssetCell.h"
#import "TransicationDetailViewController.h"
#import "TopUpViewController.h"
#import "WithdrawViewController.h"
#import "RealNameViewController.h"
#import "MyAdoptedViewController.h"
#import "DiscountCouponViewController.h"

@interface MyAssetViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTbaleView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 headerView
 */
@property (nonatomic , strong) MyAssetHeaderView *headerView;
/**
 model
 */
@property (nonatomic , strong) MyAsset *model;
/**
 明细Btn
 */
@property (nonatomic , strong) UIButton *transactionDetailBtn;
@end
@implementation MyAssetViewController


#pragma mark - view life cycle

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的资产";
    [self setUpNav];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpNav {
    //[[NavigationBarManager sharedInstance] setupNavigationBarClear:self.navigationController];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.transactionDetailBtn];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"adopt_adoptdetail_back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backTapped)];
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];

}
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
//    [[UIApplication sharedApplication].keyWindow addSubview:self.transactionDetailBtn];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(-TopBarHeight, 0, 0, 0));
    }];
//    [self.transactionDetailBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(0);
//        make.top.mas_equalTo(-38);
//        make.size.mas_equalTo(CGSizeMake(60, 32));
//    }];
}

- (void) initAction {
    [self.headerView.withdrawBtn addTarget:self action:@selector(withdrawBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView.topUpBtn addTarget:self action:@selector(topUpBtnClick) forControlEvents:UIControlEventTouchUpInside];

}

- (void)initData {
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    @weakify(self);
    [[[PigMarketApi sharedInstance] getMyAssetWithMemberId:memberId] subscribeNext:^(MyAsset * _Nullable myAsset) {
        @strongify(self);
        self.model = [[MyAsset alloc] init];
        self.model = myAsset;
        self.headerView.balance.text = [NSString stringWithFormat:@"￥%@",self.model.money];
        [self.myTableView reloadData];
    } error:^(NSError * _Nullable error) {
        
        @strongify(self);
        [self handlingError:error];
    }];
}

#pragma mark - delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyAssetCell *cell = [MyAssetCell cellWithTableView:tableView];
    [cell configCellWithModel:self.model index:indexPath];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {//预售款
        
    } else if (indexPath.row == 1) {//已领养
        [self.navigationController pushViewController:[[MyAdoptedViewController alloc] init] animated:YES];
    } else if (indexPath.row == 2) {//豚券
        [self.navigationController pushViewController:[[DiscountCouponViewController alloc] init] animated:YES];
    } else if (indexPath.row == 3) {//买肉
    } else if (indexPath.row == 4) {//奖金
    }
}
#pragma mark - target action
//明细
- (void)transicationDetailClick {
    [self.navigationController pushViewController:[[TransicationDetailViewController alloc] init] animated:YES];
}
//提现
- (void)withdrawBtnClick {
    [self toastWithContent:@"测试临时关闭实名认证校验功能"];

    self.model.attestation = @(IsRealNameStatusYes);
    if ([self.model.attestation integerValue] == IsRealNameStatusNo) {
        [[AlertAction sharedInstance] showAlertWithTitle:nil message:@"您未实名认证，为了您的资产安全，需实名认证可以提现" confirmBtnTitle:@"去实名认证" confirmBlock:^{
            [self.navigationController pushViewController:[[RealNameViewController alloc] init] animated:YES];
        }cancelBlock:nil];

    } else if ([self.model.attestation integerValue] == IsRealNameStatusYes){
        WithdrawViewController *vc = [[WithdrawViewController alloc] init];
        vc.balance = self.model.money;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
//充值                                                        
- (void)topUpBtnClick {
    [self.navigationController pushViewController:[[TopUpViewController alloc] init] animated:YES];

}
- (void)backTapped {
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - private method

#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.rowHeight = 115;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSelectionStyleNone;
        _myTableView.tableHeaderView = self.headerView;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
- (MyAssetHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[MyAssetHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200 * [JTool getScreenWidthScale])];
    }
    return _headerView;
}
- (UIButton *)transactionDetailBtn {
    if (!_transactionDetailBtn) {
        _transactionDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _transactionDetailBtn.frame = CGRectMake(0, 0, 60, 32);
        [_transactionDetailBtn setTitle:@"明细" forState:UIControlStateNormal];
        [_transactionDetailBtn addTarget:self action:@selector(transicationDetailClick) forControlEvents:UIControlEventTouchUpInside];
        [_transactionDetailBtn setBackgroundImage:[UIImage imageNamed:@"user_myasset_transicationdetail"] forState:UIControlStateNormal];
    }
    return _transactionDetailBtn;
}
@end
